package org.steven0lisa.typhoon.ui;

import java.util.ArrayList;
import java.util.List;

import net.jingkao.app.common.Logger;
import net.jingkao.app.common.UIHelper;
import net.jingkao.app.common.UIHelper.BroadcastRecieveProcessor;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.umeng.analytics.MobclickAgent;

public class BaseActivity extends Activity{
	
	protected Logger log;
	protected List<OptionsMenu> menus=new ArrayList<OptionsMenu>();
	protected BroadcastRecieveProcessor _broadcastReciever;
	
	protected BaseActivity(){
		log = Logger.getLog(this);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//执行inject操作
		UIHelper.injectAll(this);
		//_broadcastReciever = UIHelper.registerBoardcast(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		//统计
		MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		//统计
		MobclickAgent.onPause(this);
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		if(this.menus.isEmpty()){
			return false;
		}
		int index = 1;
		for(OptionsMenu m: this.menus){
			MenuItem mi = menu.add(m.group(menu),Menu.FIRST + index, index , this.getString(m.text(menu)));
			int icon = m.icon(menu);
			if(icon > 0) {
				mi.setIcon(icon);
			}
			index ++;
		}
		return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId(); 
        int index = itemId - Menu.FIRST - 1;
        if(index >= 0 && index < this.menus.size()){
        	OptionsMenu om = this.menus.get(index);
            om.onAction(item);
            return true;
        }
        return false;
	}
	
	protected void sendBroardcast(Intent intent){
		UIHelper.sendBroadcast(this, intent);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(_broadcastReciever != null){
			unregisterReceiver(_broadcastReciever);
		}
	}
	
	protected void addOptionsMenu(OptionsMenu menu){
		this.menus.add(menu);
	}
	
	static abstract class OptionsMenu{
		
		public int group(Menu menu){
			return Menu.NONE;
		}
		
		public abstract int text(Menu menu);
		
		public int icon(Menu menu){
			return android.R.drawable.ic_menu_view;
		}
		
		public abstract void onAction(MenuItem item);
	}

}
